﻿#include "Graphics32ValveItem.h"
#include <QBrush>
#include <QPen>
#include <QPainter>
#include <QDebug>
#define _USE_MATH_DEFINES
#include <cmath>
#define M_PI       3.14159265358979323846

Graphics32ValveItem::Graphics32ValveItem()
{


    m_partWidth = m_radius * 2.0;
    m_partHeight = 2.0 * m_pointsRight[1].y();

    m_width = m_partWidth;
    m_height = m_partHeight * 2.0;

    m_dir = Right;
}

QPointF Graphics32ValveItem::keyPoint(int i)
{

    switch (m_dir)
    {
    case Right:
        m_keyPoints[0] = QPoint(25, 0);
        m_keyPoints[1] = QPoint(-25, -20);
        m_keyPoints[2] = QPoint(-25, 20);

        break;
    case Left:
        m_keyPoints[1] = QPoint(25, 20);
        m_keyPoints[2] = QPoint(25, -20);
        m_keyPoints[0] = QPoint(-25, 0);
        break;
    case Up:
        m_keyPoints[1] = QPoint(-20, 25);
        m_keyPoints[2] = QPoint(20, 25);
        m_keyPoints[0] = QPoint(0, -25);
        break;
    case Down:
        m_keyPoints[2] = QPoint(-20, -25);
        m_keyPoints[1] = QPoint(20, -25);
        m_keyPoints[0] = QPoint(0, 25);
        break;
    }
    return m_keyPoints[i];
}

QRectF Graphics32ValveItem::boundingRect() const
{
    switch (m_dir)
    {
    case Right:
    case Left:
        return QRectF(-26 * scale(), -37 * scale(), 53 * scale(), 74 * scale());
        break;
    case Up:
    case Down:
        return QRectF(-37 * scale(), -26 * scale(), 74 * scale(), 53 * scale());
        break;
    }

    return QRectF(-100, -50, 200, 100);
}

void Graphics32ValveItem::setEnable(bool enable)
{
    m_enable = enable;
    update();
}

void Graphics32ValveItem::setDir(Direction dir)
{
    m_dir = dir;

    switch(m_dir)
    {
    case Left:
    case Right:
        m_width = 51;
        m_height = 74;
        break;
    case Up:
    case Down:
        m_width = 74;
        m_height = 51;
        break;
    }
    update(-37, -37, 74, 74);
}

void Graphics32ValveItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen penOn(Qt::black);
    penOn.setWidth(3);
    if(m_enable)
    {
        switch(m_dir)
        {
        case Left:
        case Right:
            painter->save();
            painter->translate(0, -20);
            drawStateOff(painter, m_dir);
            painter->translate(0, 40);
            drawStateOn(painter, m_dir);
            painter->restore();
            break;
        case Up:
        case Down:
            painter->save();
            painter->translate(-20, 0);
            drawStateOff(painter, m_dir);
            painter->translate(40, 0 );
            drawStateOn(painter, m_dir);
            painter->restore();
            break;
        }
    }
    else
    {
        switch(m_dir)
        {
        case Left:
        case Right:
            painter->save();
            painter->translate(0, -20);
            drawStateOn(painter, m_dir);
            painter->translate(0, 40);
            drawStateOff(painter, m_dir);
            painter->restore();
            break;
        case Up:
        case Down:
            painter->save();
            painter->translate(-20, 0);
            drawStateOn(painter, m_dir);
            painter->translate(40, 0);
            drawStateOff(painter, m_dir);
            painter->restore();
            break;
        }
    }
    painter->setPen(penOn);
    switch(m_dir)
    {
    case Right:
        painter->drawLine(QPoint(25, -20), QPoint(25, 20));
        break;
    case Left:
        painter->drawLine(QPoint(-25, -20), QPoint(-25, 20));
        break;
    case Up:
        painter->drawLine(QPoint(-20, -25), QPoint(20, -25));
        break;
    case Down:
        painter->drawLine(QPoint(-20, 25), QPoint(20, 25));
        break;
    }
}


